package cn.com.connext.scrm.repository.master;

import cn.com.connext.scrm.domain.master.Product;
import cn.com.connext.scrm.web.rest.resp.ProductResp;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import scala.Int;

import java.util.List;

@Repository
public interface ProductRepository extends JpaRepository<Product, String>, JpaSpecificationExecutor {
    @Transactional
    @Modifying
    @Query(value = "update product pr set pr.stock =:stock ,pr.update_time =now() where pr.stock =:stockOld and  id =:id ", nativeQuery = true)
    Integer updateStock(@Param("stock") Integer stock, @Param("stockOld") Integer stockOld,@Param("id") String id);

    Product findByCouponId(String id);

    @Query(value = "select pr.* from product pr  where pr.coupon_id =:couponId and  pr.is_delete = 0  ", nativeQuery = true)
    List<Product> findAllByCouponId(@Param("couponId") String couponId);

    @Query(value = "select * from product  where point_manager_id =:pointManagerId and is_delete = 0 and is_alive = 1  ", nativeQuery = true)
    List<Product> findByPointManagerId(@Param("pointManagerId")Integer pointManagerId);

    /**
     * 根据产品ID查询
     *
     * @param id
     * @return
     */
    Product findById(String id);

    @Query(value = "(SELECT * FROM product WHERE id = :productId)", nativeQuery = true)
    Product getProductById (@Param("productId") String productId);

    @Query(value = "(SELECT * FROM product WHERE name = :orderName)", nativeQuery = true)
    List<Product> getProductListByName(@Param("orderName") String orderName);

//    @Query(value = "(SELECT * FROM product WHERE name = :productName)", nativeQuery = true)
//    List<Product> getProductListByName (@Param("productName") String productName);

}
